package com.goods.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.converter.ProductCategoryConverter;
import com.goods.business.mapper.ProductCategoryMapper;
import com.goods.business.service.ProductCategoryService;
import com.goods.common.error.BaseError;
import com.goods.common.error.BusinessCodeEnum;
import com.goods.common.error.BusinessException;
import com.goods.common.model.business.Product;
import com.goods.common.model.business.ProductCategory;
import com.goods.common.model.system.Department;
import com.goods.common.utils.CategoryTreeBuilder;
import com.goods.common.vo.business.ProductCategoryTreeNodeVO;
import com.goods.common.vo.business.ProductCategoryVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**********************
 *
 *@Author 闫星忠
 *@Time 2021/12/15  12:00
 *
 */
@Service
public class ProductCategoryServiceImpl implements ProductCategoryService {

    @Autowired
    private ProductCategoryMapper productCategoryMapper;


    //分页展示物资类别列表以及分类树
    @Override
    public PageVO<ProductCategoryTreeNodeVO> findDepartmentList(Integer pageNum, Integer pageSize) {

//        PageHelper.startPage(pageNum, pageSize);
        List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOS = getProductCategoryTreeNodeVOS();
        //构建tree
        List<ProductCategoryTreeNodeVO> categoryTreeNodeVOS = CategoryTreeBuilder.build(productCategoryTreeNodeVOS);
//        PageInfo<ProductCategoryTreeNodeVO> info = new PageInfo<>(categoryTreeNodeVOS);

        return new PageVO<>(categoryTreeNodeVOS.size(),categoryTreeNodeVOS);
    }

//获取物资类别vo列表
    private List<ProductCategoryTreeNodeVO> getProductCategoryTreeNodeVOS() {
        List<ProductCategory> productCategories = productCategoryMapper.selectAll();
        //转VO
        return ProductCategoryConverter.converterToProductCategoryTreeNodeVO(productCategories);
    }

    //添加页，物资父级分类数据回显
    @Override
    public List<ProductCategoryTreeNodeVO> getParentCategoryTree() {
        List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOS = getProductCategoryTreeNodeVOS();
        List<ProductCategoryTreeNodeVO> categoryParentTreeNodeVOS = CategoryTreeBuilder.buildParent(productCategoryTreeNodeVOS);

        return categoryParentTreeNodeVOS;
    }

//    添加物资分类
    @Override
    public void add(ProductCategory productCategory) {
        productCategory.setCreateTime(new Date());
        productCategory.setModifiedTime(new Date());
        productCategoryMapper.insert(productCategory);
    }

//    编辑页数据回显
    @Override
    public ProductCategoryVO edit(Long id) {
        return ProductCategoryConverter.converterToProductCategoryVO(productCategoryMapper.selectByPrimaryKey(id));
    }

//    编辑页数据提交
    @Override
    public void update(Long id, ProductCategory productCategory) {
        productCategory.setId(id);
        productCategory.setModifiedTime(new Date());
        productCategoryMapper.updateByPrimaryKey(productCategory);
    }

//    删除物资分类
    @Override
    public void delete(Long id) throws BusinessException {
        Example example = new Example(ProductCategory.class);
        example.createCriteria().andEqualTo("pid",id);

        List<ProductCategory> productCategories = productCategoryMapper.selectByExample(example);
        if (!CollectionUtils.isEmpty(productCategories)) {
            throw  new BusinessException(BusinessCodeEnum.PARAMETER_ERROR,"有子分类的、和物资引用的分类不能删除！");
        }
        productCategoryMapper.deleteByPrimaryKey(id);

    }
}
